<!-- 试卷编辑弹窗 -->
<template>
  <el-dialog :title="isUpdate ? '修改试卷' : '创建试卷'" :visible="visible" width="750px" :destroy-on-close="true"
    :lock-scroll="false" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item prop="title" label="考试名称:">
        <el-input v-model="form.title" clearable placeholder="请输入考试名称" style="width: 250px" class="filter-item" />
      </el-form-item>
      <el-form-item label="选择题库:" prop="topic_ids">
        <el-select v-model="form.topic_ids" multiple :multiple-limit="3" @change="selectTopicIds()" filterable allow-create default-first-option placeholder="请选择题库标签">
          <el-option v-for="item in topicOption" :key="item.id" :label="item.title+' (分值:'+item.all_score+')'" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="试卷分值:" label-position="right" label-width="100px">
        <span style="color: red;font-size: 16px;">共计:  {{ all_score }}分</span>
      </el-form-item>
      <el-form-item label="题目顺序:" prop="is_rand" label-width="100px">
        <el-radio-group v-model="form.is_rand" size="mini">
          <!-- 状态：1 为 随机 2 固定 -->
          <el-radio-button :label="1">随机</el-radio-button>
          <el-radio-button :label="2">固定</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="考试次数:" prop="num" label-position="right" label-width="100px">
        <el-input-number v-model="form.num" :min="1" size="small" style="width: 250px" autocomplete="off"
          controls-position="right" placeholder="请输入考试上限次数" />（次数）
      </el-form-item>
      <el-form-item label="考试时长:" prop="minute" label-position="right" label-width="100px">
        <el-input-number v-model="form.minute" size="small" autocomplete="off" :min="1" style="width: 250px"
          controls-position="right" placeholder="请输入考试时长" />（分钟）
      </el-form-item>
      <el-form-item label="开始时间:" label-position="right" prop="start_time" label-width="100px">
        <el-date-picker v-model="form.start_time" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择最早考试开始时间" />
        <span style="color:red;padding-left: 20px;">(只要在时间范围内都可以进入参与考试)</span>
      </el-form-item>
      <el-form-item label="结束时间:" prop="end_time" label-position="right" label-width="100px">
        <el-date-picker v-model="form.end_time" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择考试结束时间" />
        <span style="color:red;padding-left: 20px;">(只要在时间范围内都可以进入参与考试)</span>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  name: 'ExamEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({is_rand:1, status: 1 }, this.data),
      topicOption:[],
      all_score:0,
      // 表单验证规则
      rules: {
        title: [
          { required: true, message: '请输入试卷名称', trigger: 'blur' }
        ],
        is_rand: [
          { required: true, message: '请选择出题方式', trigger: 'blur' }
        ],
        num: [
          { required: true, message: '请输入考试次数', trigger: 'blur' }
        ],
        topic_ids: [
          { required: true, message: '请选择题库', trigger: 'blur' }
        ],
        minute: [
          { required: true, message: '请输入考试时长', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择试卷状态', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序号', trigger: 'blur' }
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({ is_rand: 1 }, this.data);
        this.isUpdate = true;
      } else {
        this.form = {
          is_rand: 1
        };
        this.isUpdate = false;
      }
    }
  },
  // 页面创建时
  created() {
    this.topicExamList();
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/exam/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    selectTopicIds(){
      let all_score=0
      this.topicOption.forEach((item,index) => {
        this.form.topic_ids.forEach((topic_id,value) => {
            if(item.id==topic_id){
              all_score+=item.all_score
            }
        })
        this.all_score=all_score
        // console.log(all_score)
        // console.log("forEach循环==index==",index,item);
    })
     
    },
    /* 获取题库列表 */
    topicExamList() {
      this.$http.get('/topic/getExamTopicSelect').then(res => {
        if (res.data.code === 0) {
          this.topicOption = res.data.data;
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped></style>
